'''
002 FLY
Controls:       Flick
Description:    Touch and flick super pig into the skies
'''
import pygame
import spyral
import random  


class game002(spyral.scene.Scene):
    def __init__(self, player):
        spyral.scene.Scene.__init__(self)
        self.root_camera = spyral.director.get_camera()
        self.camera = self.root_camera.make_child(layers = [0,1,2,3])
        self.group = spyral.sprite.Group(self.camera)
        self.grabbed = False; #makes sure you can only grab it once
        
        self.player = player;
        self.pig = pig();
        self.timer = Timer.Timer(self.player.current_speed);
        
        self.counter = 0;
        self.startpos = (0,0);
        self.endpos = (0,0);
        self.throw = (0,0);
        
        self.won = False;
                
        self.group.add(self.pig, self.timer);
        
        
    def on_enter(self):
        bg = spyral.util.load_image(Dic.images['002bg'])
        self.camera.set_background(bg)
        

    def render(self):
        self.group.draw()
        self.root_camera.draw()
        
    def update(self, tick):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    spyral.director.pop()
                    exit(0)
                if event.key == pygame.K_r:
                    spyral.director.replace(game002(self.player))
            if (self.grabbed == False):
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if (self.pig.rect.collidepoint(pygame.mouse.get_pos()) == True):
                        self.pig.setGrabbed(True);
                        self.startpos = pygame.mouse.get_pos();
                if event.type == pygame.MOUSEBUTTONUP:
                    if (self.pig.getGrabbed()):
                        self.grabbed = True;
                        self.pig.setGrabbed(False);
                        self.endpos = pygame.mouse.get_pos();
                        self.throw = self.calcThrow();
        
        #pig is grabbed (count and set pos to mouse)
        if (self.pig.getGrabbed()):
            self.counter += 1;
            self.pig.setCenter(pygame.mouse.get_pos());
        
        #pig was grabbed and now let go (apply speed and gravity)    
        if (self.grabbed == True):
            self.pig.applyThrow(self.throw);
            self.pig.applyGravity()
        
        #win if you pass the right edge without being below bottom of screen
        if (960 <= self.pig.getRectLeft() and self.pig.getRectBottom() <= 640):
            self.won = True;
        
        if (self.timer.timesUp()):
            if (self.winCondition() == False):
                self.player.decHealth();
            else:
                self.player.incCurrentGamesCompleted();
            spyral.director.pop()
        
        self.group.update();
    
    
    def winCondition(self):
        return self.won;
    
    def calcThrow(self):
        x = (self.endpos[0] - self.startpos[0]) / 3;
        y = (self.endpos[1] - self.startpos[1]) / 3;
        x = x / self.counter;
        y = y / self.counter;
        return (x, y)



class pig(spyral.sprite.Sprite):
    def __init__(self):
        spyral.sprite.Sprite.__init__(self)
        self.image = spyral.util.load_image(Dic.images['002pig'])
        self.rect.topleft = (84, 178)
        self.layer = 2;
        self.grabbed = False;
        self.gravity = 5;
        
    
    def setGrabbed(self, bool):
        if (bool == True):
            oldcenter = self.rect.center;
            self.image = spyral.util.load_image(Dic.images['002pigfly'])
            self.rect.center = oldcenter;
        self.grabbed = bool;
        
    def getGrabbed(self):
        return self.grabbed;
    
    def setCenter(self, pos):
        self.rect.center = pos;
        
    def applyGravity(self):
        self.rect.center = (self.rect.center[0], self.rect.center[1] + self.gravity)
        self.gravity += 1;
    
    def applyThrow(self, throw):
        self.rect.center = (self.rect.center[0] + throw[0], self.rect.center[1] + throw[1])
        
    def getRectLeft(self):
        return self.rect.left;

    def getRectBottom(self):
        return self.rect.bottom;

        



if __name__ == "__main__":
    import sys
    sys.path.append("C:\Users\Patrick\paybackpigs\paybackpigs")
    from frenzy import Dic
    Dic.setImgFolder('../images/')
    from frenzy import Image
    from frenzy import Timer
    from frenzy import Player
    spyral.init()
    spyral.director.init(Dic.geom['size'], False, 'Payback Pigs', 30, 30)
    spyral.director.push(game002(Player.Player()))
    spyral.director.run()

else:
    import Image
    import Dic
    import Timer;